package cn.xaut.动态规划;

/**
 * 322. 零钱兑换
 */
public class demo322 {

    public int coinChange(int[] coins, int amount) {

        // dp[i] 表示当金额为 i 时，至少需要 dp[i] 枚硬币
        int[] dp = new int[amount + 1];
        // 初始化
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) 
            dp[i] = amount + 1;
        
        // 状态转移
        for (int i = 1; i <= amount; i++) {
            for (int coin : coins) {
                if (i < coin)
                    continue;
                dp[i] = Math.min(dp[i], dp[i - coin] + 1);
            }
        }
        
        return dp[amount] == amount + 1 ? -1 : dp[amount];
    }

    public static void main(String[] args) {

        System.out.println(new demo322().coinChange(new int[]{1, 2, 5}, 11));// 3
        System.out.println(new demo322().coinChange(new int[]{2}, 3));// -1
        System.out.println(new demo322().coinChange(new int[]{1}, 0));// 0
        System.out.println(new demo322().coinChange(new int[]{1}, 1));// 1
        System.out.println(new demo322().coinChange(new int[]{1}, 2));// 2
    }
}
